-- CaveHideItem
-- Created by chengb Apr/9/2016
-- 洞窟隐藏物件格子

-- 初始化
function initCaveHideItem(self)
    local class = self.gridData.class;

    if class == 798 then
        -- 生锈培养炉
        initRustyCabin(self);
        return;
    end

    local buildingInfo = BuildingBonusM.queryBuildingView(class);
    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    local function okFunc()
        local pos = self.gridData:getPos();
        DungeonActionM.go("do_building_bonus", pos);
        EventMgr.fire(event.PLAYER_MOVE, pos);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("button_spell");

            -- 还有怪物未死亡
            if not DungeonM.isAllMonsterDead() then
                self:openBuildingView(nil, -20, getLocStr("cave_hide_item_dialog"), getLocStr("btn_text_leave"));
                return;
            end

            -- 打开建筑查看界面
            self:openBuildingView(okFunc);
        end
    end
    self.icon:addTouchEventListener(onClicked);

    -- 发光
    if self.gridData.need_shining == 1 then
        shiningEffect(self.icon, 70, 100);
    end
end

-- 初始化生锈培养炉
function initRustyCabin(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    self.times = self.times or 1;

    local function doBomEffect()
        -- 爆炸光效
        playEffect(self, 1233, 0, 20);
    end

    -- 操作未达到时的点击效果
    local function okFunc1()
        self.times = self.times + 1;
        alert(buildingInfo["fail_tip"])
    end

    -- 操作次数达到了
    local function okFunc2()
        local pos = self.gridData:getPos();
        DungeonActionM.go("do_building_bonus", pos);
        performWithDelay(self, doBomEffect, 0.2);
        EventMgr.fire(event.PLAYER_MOVE, pos);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("button_spell");

            -- 还有怪物未死亡
            if not DungeonM.isAllMonsterDead() then
                self:openBuildingView(nil, -20, getLocStr("cave_hide_item_dialog"), getLocStr("btn_text_leave"));
                return;
            end

            -- 打开建筑查看界面
            if self.times >= 10 then
                self:openBuildingView4(okFunc2, okFunc2, okFunc2, okFunc2,
                    getLocStr("btn_text_cabin_1"), getLocStr("btn_text_cabin_2"), getLocStr("btn_text_cabin_3"), getLocStr("btn_text_cabin_4"));
            else
                self:openBuildingView4(okFunc1, okFunc1, okFunc1, okFunc1,
                    getLocStr("btn_text_cabin_1"), getLocStr("btn_text_cabin_2"), getLocStr("btn_text_cabin_3"), getLocStr("btn_text_cabin_4"));
            end
        end
    end
    self.icon:addTouchEventListener(onClicked);

    -- 发光
    if self.gridData.need_shining == 1 then
        shiningEffect(self.icon, 70, 100);
    end
end